//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    height: app.globalData.height,
    width: app.globalData.width,
    barHeight: app.globalData.barHeight,
    serverUrl: app.globalData.serverUrl,
    input: '',
    picItems: [],
    noticeItems: []
  },
  //事件处理函数
  onLoad: function() {
    wx.showShareMenu({
      withShareTicket: true
    })
  },
  onShow: function(){
    this.getCarousel();
    this.getNotice();
  },
  search: function(e) {
    if (this.data.input == '') {

    } else {
      wx.navigateTo({
        url: '/pages/search/search?search=' + this.data.input,
        complete: res => {
          this.setData({
            input: '',
          })
        }
      })
    }
  },
  help: function(e) {
    wx.navigateTo({
      url: '/pages/help/help',
    })
  },
  teacher: function(e) {
    wx.navigateTo({
      url: '/pages/teacher/teacher',
    })
  },
  express: function(e) {
    wx.navigateTo({
      url: '/pages/express/express',
    })
  },
  sale: function(e) {
    wx.navigateTo({
      url: '/pages/sale/sale',
    })
  },
  input: function(e) {
    this.setData({
      input: e.detail.value
    })
  },
  //获取轮播图
  getCarousel: function() {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + '/admin/index/getCarousel',
      method: 'POST',
      data: {},
      success(res) {
        that.setData({
          picItems: res.data.paths.split(','),
          publish: res.data.publish
        });
      }
    })
  },
  //获取公告
  getNotice: function() {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + '/admin/notice/getnotice',
      method: 'POST',
      data: {},
      success(res) {
        var length = res.data.notice.length;
        for (var i = 0; i < length; i++) {
          try {
            res.data.notice[i].paths = res.data.notice[i].paths.split(',');
          } catch (e) {
            res.data.notice[i].paths = ['/images/scnu.png'];
          }
        };
        that.setData({
          noticeItems: res.data.notice,
        });
      }
    })
  },

  // 去发布页面
  toIssue() {
    wx.navigateTo({
      url: '/pages/issue/issue',
    })
  },

  //获取快递详情
  getDetail: function(e) {
    let item = e.currentTarget.dataset.item;
    item = JSON.stringify(item);

    wx.navigateTo({
      url: '/pages/notice/notice?item=' + item,
    })
  },
})